﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xna_game_engine.source
{
    public class MasterItemList
    {
        private string mItemPath;
        private ItemDefinition mItemDef;
        private List<BaseItem> mItemList;

        public MasterItemList()
        {
            mItemPath = "../../../XML/items/";
            XmlSerializer serializer = new XmlSerializer(typeof(ItemDefinition));
            mItemList = new List<BaseItem>();
            // Recurse into subdirectories of this directory.
            string[] files = Directory.GetFiles(mItemPath);
            foreach (string file in files)
            {
                Stream stream = new FileStream(file, FileMode.Open);
                using (stream)
                {
                    mItemDef = (ItemDefinition)serializer.Deserialize(stream);
                    BaseItem temp = CreateItemFromDefinition(mItemDef);
                    if (false == mItemList.Contains(temp))
                    {
                        mItemList.Add(temp);
                    }
                    Console.WriteLine("Found file:" + file);
                }
            }
        }

        private BaseItem CreateItemFromDefinition(ItemDefinition def)
        {
            BaseItem temp = new BaseItem(mItemDef.mName);
            temp.Strength = def.mStrength;
            temp.Agility = def.mAgility;
            temp.Power = def.mPower;
            temp.Speed = def.mSpeed;
            // TODO: the rest of the info
            return temp;
        }

        public BaseItem GetItem(int level)
        {
            // TODO: Check level to return appropriate item
            int randIndex = GameGlobals.RNG.Next(mItemList.Count);
            return mItemList[randIndex].Clone();
        }
    }
}
